#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

#define CONTACTS_MAX_SIZE 100

#define DEBUG

#ifdef DEBUG
  #define LOG_DEBUG printf
#else
  #define LOG_DEBUG
#endif

/**
 * 字符串转int
 */
int str2int(char *str){
    int temp = 0;
    char *ptr = str;
    if (*str == '-' || *str == '+'){
        str++;
    }
    while(*str != 0){
        if ((*str < '0') || (*str > '9')) {
            break;
        }
        temp = temp * 10 + (*str - '0');
        str++;
    }
    if (*ptr == '-'){
        temp = -temp;
    }
    return temp;
}

/**
 * 通讯录结构体
 */
typedef struct ct{
  char name[20];
  char company[20];
  char phone[20];
  char email[30];
} Contact;

/**
 * 打印通讯录
 */
void ctPrint(Contact *contact){
  printf("name:\t\t%s\ncompany:\t%s\nphone:\t\t%s\nemail:\t\t%s\n",contact->name,contact->company,contact->phone,contact->email);
}

/**
 * 所有的通讯录
 */
typedef struct cts{
  int ptr;
  Contact *contacts[CONTACTS_MAX_SIZE];
} Contacts;

/**
 * 插入数据
 */
int ctsPush(Contacts *contacts,char *name,char *company,char *phone,char *email){
  LOG_DEBUG("add contact\n");
  if(contacts->ptr >= CONTACTS_MAX_SIZE){
    return 1;
  }
  Contact *contact = (Contact *)malloc(sizeof(Contact));
  strcpy(contact->name,name);
  strcpy(contact->company,company);
  strcpy(contact->phone,phone);
  strcpy(contact->email,email);
  contacts -> contacts[contacts->ptr++] = contact;
  return 0;
}

/**
 * 修改数据
 */
int ctsEdit(Contacts *contacts,char *loc,char *name,char *company,char *phone,char *email){
  LOG_DEBUG("edit contact %s\n",loc);
  int loc_ = str2int(loc);
  Contact *contact = contacts -> contacts[loc_];
  strcpy(contact->name,name);
  strcpy(contact->company,company);
  strcpy(contact->phone,phone);
  strcpy(contact->email,email);
}

/**
 * 选择数据
 */
Contact *ctsSelect(Contacts *contacts,char *loc){
  LOG_DEBUG("select contact %s\n",loc);
  int loc_ = str2int(loc);
  return contacts -> contacts[loc_];
}

/**
 * 删除最后一个
 */
int ctsDeleteLast(Contacts *contacts){
  if( 0 == contacts -> ptr){
    return 1;
  }
  free(contacts->contacts[contacts->ptr --]);
  return 0;
}

/**
 * 打印所有的数据
 */
void ctsPrintAll(Contacts *contacts){
  int i = 0;
  printf("contacts all content is:\n");
  printf("---------------------------\n");
  for(;i< contacts->ptr; i++){
    ctPrint(contacts ->contacts[i]);
    printf("\n");
  }
  printf("---------------------------\n");
}

/**
 * 主函数
 */
int main(int argc,char *argv[]){
  int i = 0;
  Contacts *contacts = (Contacts *)malloc(sizeof(Contacts));
  contacts -> ptr = 0;
  ctsPush(contacts,"first name","first company","12345678","first@126.com");
  ctsPush(contacts,"second name","second company","12345678","first@126.com");
  #ifdef DEBUG
    LOG_DEBUG("input args is:\n");
    for (i=0 ; i<argc ;i++){
      LOG_DEBUG("args %d -> %s\n",i,argv[i]);
    }
  #endif
  ctsPrintAll(contacts);
  if(!strcmp(argv[1],"add")){
    ctsPush(contacts,argv[2],argv[3],argv[4],argv[5]);
  }else if(!strcmp(argv[1],"edit")){
    ctsEdit(contacts,argv[2],argv[3],argv[4],argv[5],argv[6]);
  }else if(!strcmp(argv[1],"del")){
    ctsDeleteLast(contacts);
  }else if(!strcmp(argv[1],"select")){
    ctPrint(ctsSelect(contacts,argv[2]));
  }
  ctsPrintAll(contacts);
  return 0;
}